// pages/login/login.js
var utils = require('../../utils/util.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isChecked: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var typeNmae = "";
    var defalut = "";
    if (options.logintype == 2) {
      typeNmae = "果农"
      defalut = wx.getStorageSync('farmerend-access')
    }
    if (options.logintype == 1) {
      typeNmae = "商户"
      defalut = wx.getStorageSync('business-access')
    }
    console.log('dddddd', defalut)
    this.setData({
      logintype: options.logintype,
      typeNmae: typeNmae,
      defalut: defalut,
      isChecked: defalut ? true : false
    })
    this.systemList();
  },
  //基本数据
  systemList: function(e) {
    var that = this;
    utils.http("/api/v1/common/system", {}, function(res) {
      console.log(res);
      var typeContent = "";
      if (that.data.logintype == 2) {
        typeContent = res.datas.growers_login
      }
      if (that.data.logintype == 1) {
        typeContent = res.datas.shop_login
      }
      that.setData({
        typeContent: typeContent
      })
    })
  },
  //果农端
  // formSubmit:function(e){
  //     wx.reLaunch({
  //         url: '../farmerend/farmerend',
  //     })
  // },
  //商户端
  // formSubmit: function(e) {
  //     wx.reLaunch({
  //         url: '../business/business',
  //     })
  // },
  formSubmit: function(e) {
    var that = this;
    var nameuser = e.detail.value.nameuser;
    var password = e.detail.value.password;
    utils.showLoading();
    if ((/^1(3|4|5|7|8)\d{9}$/.test(nameuser)) && password != "") {
      utils.http("api/v1/storemanage/login", {
        username: nameuser,
        password: password
      }, function(res) {
        utils.hideLoading();
        if (res.state == 1) {
          wx.setStorageSync("userType", {
            type: res.datas.type,
            storeid: res.datas.id
          });
          if (res.datas.type == 1) {
            if (that.data.logintype == 1) {
              //1  商户
              if (that.data.isChecked) {
                wx.setStorage({
                  key: 'business-access',
                  data: {
                    username: nameuser,
                    password: password
                  },
                })
              } else {
                wx.removeStorage({
                  key: 'business-access',
                })
              }
              wx.reLaunch({
                url: '../business/business',
              })
            } else {
              wx.showToast({
                title: '您的是商户账户哦~',
                icon: "none"
              })
            }
          }
          if (res.datas.type == 2) {
            if (that.data.logintype == 2) {
              //2 果农
              if (that.data.isChecked) {
                wx.setStorage({
                  key: 'farmerend-access',
                  data: {
                    username: nameuser,
                    password: password
                  },
                })
              } else {
                wx.removeStorage({
                  key: 'farmerend-access',
                })
              }
              wx.reLaunch({
                url: '../farmerend/farmerend',
              })
            } else {
              wx.showToast({
                title: '您的是果农账户哦~',
                icon: "none"
              })
            }
          }
        } else {
          utils.message(res.datas)
        }
      })
    } else {
      if (password == "") {
        wx.showToast({
          title: '您的密码不正确哦~',
          icon: "none"
        })
      }
      if (!(/^1(3|4|5|7|8)\d{9}$/.test(nameuser))) {
        wx.showToast({
          title: '您的账户不正确哦~',
          icon: "none"
        })
      }

    }
  },
  checkboxChange(e) {
    if (e.detail.value.length > 0) {
      this.setData({
        isChecked: true
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})